package com.elite.groovy.callback

import com.elite.groovy.util.EliteBean;
import com.elite.groovy.kh.KHUtil
import com.elite.groovy.util.EliteBean
import org.apache.commons.logging.Log
import org.apache.commons.logging.LogFactory

import javax.servlet.http.HttpServletRequest
import javax.servlet.http.HttpServletResponse
import javax.sql.DataSource

/**
 * @use 自定义问卷自动启用：根据前端配置自定义问卷启用时间，同一个回访任务类型启用唯一一套问卷
 * @author admin
 */

public class AutoQuestion implements ServiceBackInvoke {
    private static final Log log = LogFactory.getLog(AutoQuestion.class);


    public String doCallBack(EliteBean eliteBean, HttpServletRequest request, HttpServletResponse response){
        try {
            List<Map<String, Object>> AutoQuestionList = eliteBean.chaxun_sql(" Select * From ( Select q.Questionnaire_Id, q.Title, q.Questionnaire_Type, c.Mapname, q.Opentime, q.Status, Row_Number() Over(Partition By q.Questionnaire_Type Order By q.Opentime Desc) As Rn From Questionnaire_List q Left Join Callbacktask c On c.Map_Id = q.Questionnaire_Type ) Where Rn = 1 And Opentime = To_Date(To_Char(Sysdate, 'yyyy/mm/dd'), 'yyyy/mm/dd')", "dataSource");
            log.debug("【自定义问卷自动启用，AutoQuestionList】" + AutoQuestionList);

            if (AutoQuestionList != null && AutoQuestionList.size() > 0) {
                for (def i = 0; i < AutoQuestionList.size(); i++) {
                    Map<String, Object> AutoQuestion = AutoQuestionList[i];
                    log.debug("【自定义问卷，AutoQuestion】" + AutoQuestion);
                    String Questionnaire_Id = AutoQuestion.get("Questionnaire_Id"); //问卷id
                    String Questionnaire_Type = AutoQuestion.get("Questionnaire_Type"); //问卷类型
                    def updatesqlresult1 = eliteBean.update("Update Questionnaire_List Set Status = 0, Modifyby = 'SELITE', Modifydate = Sysdate Where Questionnaire_Type = '" + Questionnaire_Type + "'", "dataSource");
                    def updatesqlresult2 = eliteBean.update("Update Questionnaire_List Set Status = 1, Modifyby = 'SELITE', Modifydate = Sysdate Where Questionnaire_Id = '" + Questionnaire_Id + "'", "dataSource");
                    log.debug("updatesqlresult:" + updatesqlresult1 + "；" + updatesqlresult2);
                }
            }

        } catch (Exception e) {
            log.debug("----------AutoQuestion(自定义问卷自动启用)异常----------");
            log.error("", e);
        }
        return "1";
    }

}
